package cn.bigfire.wedesk.service.bo;

import cn.bigfire.wedesk.action.vo.WebInfo;
import cn.bigfire.wedesk.bean.Wedesk;
import cn.bigfire.wedesk.net.protocol.Protocol;
import com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;

import java.net.InetSocketAddress;

public class Context {

    //连接即可获得
    ChannelHandlerContext ctx;//netty Context对象
    Channel channel;//netty Channel对象

    String channelId;//netty ChannelId
    String clientIp;//客户端的ip信息
    int clientPort;//客户端的端口信息


    Wedesk.PCInfo pcInfo;
    WebInfo webInfo;

    public void printInfo(){
        if (pcInfo != null) {
            System.out.println(JSON.toJSONString(pcInfo));
        }
        if (webInfo != null) {
            System.out.println(JSON.toJSONString(webInfo));
        }
    }


    int cmd;//当前协议号，每次请求进来会更换此cmd
    CustInfo custInfo;  //登录之后才会有此对象

    public Context(){}

    public Context(ChannelHandlerContext ctx) {
        this.ctx = ctx;
        this.channel = ctx.channel();
        this.channelId = ctx.channel().id().asLongText();
        InetSocketAddress client = (InetSocketAddress) ctx.channel().remoteAddress();
        this.clientIp = client.getAddress().getHostAddress();
        this.clientPort = client.getPort();
    }

    public void send(Protocol protocol) {
        if (protocol.getCmd() == 0) {
            protocol.setCmd(cmd);
        }
        channel.writeAndFlush(protocol);
    }


    public ChannelHandlerContext getCtx() {
        return ctx;
    }

    public void setCtx(ChannelHandlerContext ctx) {
        this.ctx = ctx;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    public String getClientIp() {
        return clientIp;
    }

    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }

    public int getClientPort() {
        return clientPort;
    }

    public void setClientPort(int clientPort) {
        this.clientPort = clientPort;
    }

    public Wedesk.PCInfo getPcInfo() {
        return pcInfo;
    }

    public void setPcInfo(Wedesk.PCInfo pcInfo) {
        this.pcInfo = pcInfo;
    }

    public WebInfo getWebInfo() {
        return webInfo;
    }

    public void setWebInfo(WebInfo webInfo) {
        this.webInfo = webInfo;
    }

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public CustInfo getCustInfo() {
        return custInfo;
    }

    public void setCustInfo(CustInfo custInfo) {
        this.custInfo = custInfo;
    }
}
